package main

import "fmt"

type Human struct {
	name  string
	age   int
	phone string
}

type Student struct {
	Human  //匿名字段
	school string
	loan   float32
}

type Employee struct {
	Human   //匿名字段
	company string
	money   float32
}

//Human的SayHi方法
func (h Human) SayHi() {
	fmt.Printf("你好，我叫%s，你可以打我电话%s\n", h.name, h.phone)
}

//Human的Sing方法
func (h Human) Sing(lyrics string) {
	fmt.Println("La la la ...", lyrics)
}

//Human的Guzzle方法
func (h Human) Guzzle(beerStein string) {
	fmt.Println("Guzzle Guzzle Guzzle...", beerStein)
}

//重写Employee继承的SayHi方法
func (e Employee) SayHi() {
	fmt.Printf("你好，我叫%s，在%s工作，你可以打我电话%s\n", e.name, e.company, e.phone)
}

//Student实现BorrowMoney方法
func (s Student) BorrowMoney(amount float32) {
	s.loan += amount
}

//Employee实现SpendSalary方法
func (e Employee) SpendSalary(amount float32) {
	e.money -= amount
}

//定义interface
//Human、Student、Employee实现
type Men interface {
	SayHi()
	Sing(lyrics string)
	Guzzle(beerStein string)
}

//Human、Student实现
type YoungChap interface {
	SayHi()
	Sing(song string)
	BorrowMoney(amount float32)
}

//Human、Employee实现
type ElderlyGent interface {
	SayHi()
	Sing(song string)
	SpendSalary(amount float32)
}

//任意的类型都实现了空interface(我们这样定义：interface{})，也就是包含0个method的interface
/*
interface就是一组抽象方法的集合，它必须由其他非interface类型实现，而不能自我实现
*/
func main() {
	mike := Student{Human{"Mike", 25, "222-222-XXX"}, "MIT", 0.00}
	paul := Student{Human{"Paul", 26, "111-222-XXX"}, "Harvard", 100}
	sam := Employee{Human{"Sam", 36, "444-222-XXX"}, "Golang Inc.", 1000}
	tom := Employee{Human{"Tom", 37, "222-444-XXX"}, "Things Ltd.", 5000}

	//定义interface 类型Men类型变量
	var i Men
	//i可存储Student
	i = mike
	fmt.Println("mike是Student: ")
	i.SayHi()
	i.Sing("November rain")

	//i可存储Employee
	i = tom
	fmt.Println("tom是Employee: ")
	i.SayHi()
	i.Sing("Born to be wild")

	//定义了slice Men
	fmt.Println("定义一个Men类型slice")
	mens := make([]Men, 3)
	//这三个都是不同类型元素，但是他们实现了同一个interface Men，都可以放到[]Men slice中
	mens[0], mens[1], mens[2] = paul, sam, mike
	for _, value := range mens {
		value.SayHi()
	}
}
